﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TerhelesEloszto.Common;
using TerhelesEloszto.Engine;

namespace Presentation
{
	public partial class TeacherSettings : System.Web.UI.Page
	{
		private ITeacher me;

		private static IEngine engine = Engine.Instance();

		protected void Page_Load( object sender, EventArgs e )
		{
			try
			{
				String visitorName = User.Identity.Name;
				if( !engine.IsTeacher( visitorName ) )
				{
					if( engine.IsAdmin( visitorName ) )
					{
						Response.Redirect( "~/Teacher/AdminProblem.aspx" );
					}
					else
					{
						Response.Redirect( "~/Problem.aspx" );
					}

				}
				else
				{
					me = engine.GetTeacherProfile( visitorName );

					if( !Page.IsPostBack )
					{
						UserName.Text = me.Name.Trim();
						Email.Text = me.Email.Trim();
					}
				}
			}
			catch( EngineException ex )
			{
				Response.Redirect( "~/Teacher/Error.aspx" );
			}
		}

		protected void ModifySettings_Click( object sender, EventArgs e )
		{
			try
			{
				engine.UpdateUser( me, UserName.Text, me.PasswordHash, Email.Text );
				me = engine.GetTeacherProfile( User.Identity.Name );
				Response.Redirect( "~/Teacher/TeacherDefault.aspx?message=ok" );
			}
			catch( EngineException ex )
			{
				Response.Redirect( "~/Teacher/Error.aspx" );
			}
		}

		protected void ChangePassword_Click( object sender, EventArgs e )
		{
			try
			{
				engine.UpdateUser( me, me.Name, engine.CreateHash( Password.Text ), me.Email );
				me = engine.GetTeacherProfile( User.Identity.Name );
				Response.Redirect( "~/Teacher/TeacherDefault.aspx?message=ok" );
			}
			catch( EngineException ex )
			{
				Response.Redirect( "~/Teacher/Error.aspx" );
			}
		}
	}
}